var regbtn=document.getElementById("btn");
regbtn.onclick=function () {
    var telValue=email.value;
    var pwdValue=pwd.value;
    var yzmValue=yzm.value;
    var telIsRight=false;
    var pwdRight=false;
    var telReg=/^1[3-9][0-9]{9}$/;
    //手机号验证
    if(telValue.length==0){
        error1.innerHTML="此项必须填写";
        telIsRight=false;
    }else if(!telReg.test(telValue)){
        error1.innerHTML="手机号无效";
        telIsRight=false;
    }else{
        telIsRight=true;
    }
    //密码验证
    if(pwdValue.length==0){
        error2.innerHTML="此项必须填写";
        error2.className="error";
        pwdRight=false;
        pwd.className="";
    }else if(pwdValue.length<6){
        error2.innerHTML="密码至少6位，支持数字、字母和符号";
        error2.className="error";
        pwdRight=false;
        pwd.className="";
    }else if(pwdValue.length<8){
        error2.innerHTML="强度高的密码至少需要8个字符，同时包含大小写字母、数字和符号。";
        error2.className="warn";
        pwdRight=true;
        pwd.className="warnbg";
    }else{
        error2.innerHTML="";
        error2.className="error";
        pwd.className="";
        pwdRight=true;
    }

    if(yzmValue.length==0){
        error3.innerHTML="此项必须填写";
    }else{
        $.ajax({
            type:"POST",
            url:baseurl+"/bijiyzm/find2",
            contentType:"application/json; charset=utf-8",
            data:JSON.stringify({
                tel:email.value,
                yzm:yzm.value
            }),
            success:function (data) {
                if(!data){
                    error3.innerHTML = "验证码错误！";
                }else{
                    if(telIsRight&&pwdRight){
                        $.ajax({
                            type:"POST",
                            url:baseurl+"/bijiuser/add",
                            contentType : "application/json; charset=utf-8",
                            data:JSON.stringify( {
                                "createData":new Date().toLocaleString(),
                                "pwd":pwd.value,
                                "tel":email.value
                            }),
                            success:function (data) {
                                if(data){
                                    tip.innerHTML = "注册成功！";
                                    location="login.html";
                                }else{
                                    tip.innerHTML = "发生错误！";
                                }
                            }
                        })
                    }
                }
            }
        })
    }

};


email.onfocus=function () {
    error1.innerHTML="";
};

email.onblur = function(){
    $.ajax({
        type:"POST",
        url:baseurl+ "/bijiuser/checktel",
        contentType:"application/json;charset=utf-8",
        data:JSON.stringify({
            "tel": email.value
        }),
        success:function(data){
            error1.innerHTML = data?"":"手机号已经注册！";
        }
    });
};

pwd.onfocus=function () {
    error2.innerHTML="";
};

yzm.onfocus=function () {
    error3.innerHTML="";
};


var issend=false;
getyzm.onclick=function () {
    if(issend){
        return;
    }
    issend=true;

    var i=60;
    getyzm.innerHTML=i+"s重新获取";
    var timer=setInterval(function () {
        i--;
        if(i==0){
            getyzm.innerHTML="获取验证码";
            clearInterval(timer);
            return;
        }
        getyzm.innerHTML=i+"s重新获取";
    },1000);

    $.ajax({
        type:"POST",
        url:baseurl+"/bijiyzm/add",
        contentType:"application/json; charset=utf-8",
        data:JSON.stringify({
            tel:email.value
        }),
        success:function (data) {
            if(data){
                tip.innerHTML = "短信已经发送！";
            }else{
                tip.innerHTML = "短信发送失败！";
            }
        }
    })
};